<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Smarty;

use Srv\Libs\Common\CommNet;
use Srv\Libs\Common\CommTime;
use Srv\Libs\Tools\IpAddr;

final class SmartyPluginModifier
{
    /**
     * @param $ipStr
     * @return string
     * getIpStrLocation
     */
    public static function getIpStrLocation($ipStr):string
    {
        $ipStr      = strval($ipStr);
        if(strlen($ipStr) < 1) return '';
        return IpAddr::getLocationInfo($ipStr)['country'];
    }

    /**
     * @param $ipInt
     * @return string
     * getIpLongLocation
     */
    public static function getIpLongLocation($ipInt):string
    {
        $ipInt      = strval($ipInt);
        if(strlen($ipInt) < 1) return '';
        return IpAddr::getLocationInfo(CommNet::longToIp(intval($ipInt)))['country'];
    }

    /**
     * @param $ipInt
     * @return string
     * getIpStr
     */
    public static function getIpStr($ipInt):string
    {
        $ipInt      = strval($ipInt);
        return strlen($ipInt) > 0 ? CommNet::longToIp(intval($ipInt)) : '';
    }

    /**
     * @param $ipStr
     * @return int
     * getIpLong
     */
    public static function getIpLong($ipStr):int
    {
        $ipStr      = strval($ipStr);
        return strlen($ipStr) > 0 ? CommNet::ipToLong($ipStr) : 0;
    }

    /**
     * @param $timeStamp
     * @param string $type
     * @return string
     * getDateTime
     */
    public static function getDateTime($timeStamp, string $type = 'datetime'):string
    {
        $timeStamp          = intval($timeStamp);
        strlen($type) < 1 && $type = 'datetime';
        switch($type){
            case 'date' :
            case 'log' :{
                $resStr     = CommTime::getDate($timeStamp);
                break;
            }
            case 'time' :{
                $resStr     = CommTime::getTime($timeStamp);
                break;
            }
            case 'gmt' :{
                $resStr     = CommTime::getGmt($timeStamp);
                break;
            }
            case 'datetime' :{
                $resStr     = CommTime::getDateTime($timeStamp);
                break;
            }
            case 'string' :{
                $resStr     = CommTime::getTimeSting($timeStamp);
                break;
            }
            default:{
                $resStr     = CommTime::getString($type, $timeStamp);
            }
        }
        return $resStr;
    }
}